

package instrumentation;

import java.util.BitSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import soot.SootClass;
import soot.SootField;

public class Factory {

	public static final Factory singleton = new Factory();

	private final Map<SootClass, ConditionClass> classes;
	private final Map<SootField, Data> fieldRepo;

	private final BitSet fieldsMask;

	private Factory() {
		classes = new HashMap<SootClass, ConditionClass>();
		fieldRepo = new HashMap<SootField, Data>();
		fieldsMask = new BitSet();
	}

	public BitSet getFieldsMask() {
		return fieldsMask;
	}



	public Set<Data> get( Collection<SootField> fields) {
		Set<Data> ret = new HashSet<Data>(fields.size());

		for(SootField f : fields)
			ret.add(get(f));

		return ret;
	}

	public Data get(SootField field) {
		String fTpe=field.getType().toString();
		Data d = fieldRepo.get(field);
		if(d == null) {
			d = new Data(field.getName(),fTpe,-1, false);
			fieldRepo.put(field, d);
		}
		return d;
	}
	public ConditionClass get(SootClass sClass, Set<Data> fields) {
		ConditionClass ret = classes.get(sClass);
		if(ret == null) {
			ret = new ConditionClass(sClass.getName(), fields);
			classes.put(sClass, ret);
		}
		return ret;
	}
	public Collection<ConditionClass> getClasses() {
		return classes.values();
	}

}
